<?php 
namespace app\index\controller;
use think\Controller;

use think\Db;//导入数据库类 (DB类)
use think\Session;
use think\Request;
use app\index\model\User;

class User extends Controller
{

    // ===================登录=========================================
    public function login()
    {
        return view('User/login');
    }
    public function loginP()
    {

        if(request()->isPost()){
            // 查询数据
            $captcha = new \think\captcha\Captcha();
            if (!$captcha->check(input('seccodeverify'))) {
            return 'code';
            } 
            $user = db('user')->where( 'tel',input('username'))->find();
            if($user == ''){
                return 'error';
            }
            if($user['status']==0){
                return 'stat';
            }
            $pwd = db('user')->where('id',$user['id'])->where('pwd',input('password'))->select();

        
   
            // 判断是否登录成功,并保存session
            if ($user && $pwd ) {
                Session::set('user',$user);
                return 'ok';
            }else{
                return 'no';
                
            }
        }
        
        
    }
    public function loginF()
    {
        // 查询数据
        $user = db('user')->where( 'tel',input('username'))->find();
        $pwd = db('user')->where('pwd',input('password'))->select();
        // 判断是否登录成功,并保存session
        if ($user && $pwd) {
            Session::set('user',$user);
            $this->success('登录成功','Index/index');
        }else{
            return '<script>alert("登录失败,请重试");window.location="indexLogin"</script>';
            
        }
    }
    public function yzm()
    {

       return captcha_check(input('post.seccodeverify'));

    }
    // =================退出登录==========================================
    public function loginO()
    {
        Session::clear();
        $this->redirect('index/Index/index');

    }
    // ===================注册===========================================
    public function register()
    {
        return view('User/register');
    }
    public function registerP()
    {
        // 获取ajax传递过来的数据

            $data = [
                'tel'=>input('post.mobile'),
                'nickname'=>input('post.m_nickname'),
                'pwd'=>input('post.m_password'),
                'regtime'=>time(),
                'icon'=>'static/images/none1.gif'            
            ];
     
        // 进行查询后返回
            
            if(Request::instance()->isPost()){
                $captcha = new \think\captcha\Captcha();
            if (!$captcha->check(input('seccodeverify'))) {
            return 'code';
            } 
                    if(db('user')->where('tel',$data['tel'])->select() ){
                        return 'error_tel';
                        exit;
                    }
                    if( db('user')->where('nickname',$data['nickname'])->select() ){
                        return 'error_nick';
                    }
                    if(db('user')->insert($data)){
                        return 'ok';
                        exit;
                    }else{
                        return 'no';
                        exit;
                    }
            }
    }
}


        
   

